package com.petmall.service;

import com.petmall.entity.Item;
import com.petmall.entity.Review;
import com.petmall.mapper.ItemMapper;
import com.petmall.util.ItemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by pig2guang on 2017/4/10.
 */
@Service
public class ItemService {

    @Autowired
    private ItemMapper itemmapper;

    public List<Item> showAllItems(){
        int status = new ItemUtil().status_sale_in;
        return itemmapper.selectItemByStatus(status);
    }

    public List<Item> showClassifiedItems(int category){
        int status = new ItemUtil().status_sale_in;
        return itemmapper.selectItemByCategory(category, status);
    }

    public Item selectItemById(long item_id){
        return itemmapper.selectItemById(item_id);
    }

    public List<Review> listItemReviews(long item_id){
        return itemmapper.selectItemReviewById(item_id);
    }

    public String getItemCategory(int category_id){
        return itemmapper.selectItemCategoryNameById(category_id);
    }

    //添加新商品
    public void addItem(Item item){
        itemmapper.insertItem(item.getCategory(), item.getName(), item.getTitle(), item.getDescription(), item.getPrice(), new ItemUtil().status_sale_in, item.getNum(), new ItemUtil().item_sales_normal, new ItemUtil().item_image_normal, getCurrentTime());
    }

    //添加新商品
    public void modifyItemInfo(Item item){
        System.out.println("id:" + item.getItem_id() + ",category:" + item.getCategory() + ",name:" + item.getName() + ",title:"+ item.getTitle() + ",description:"+ item.getDescription() + ",price:"+ item.getPrice() + ",num:"+ item.getNum());
        itemmapper.updateItemInfo(item.getItem_id(),item.getCategory(), item.getName(), item.getTitle(), item.getDescription(), item.getPrice(),item.getNum());
    }

    //获取信息最新时间
    private String getCurrentTime(){
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time ;
    }

}
